Javascript Generator
n日後の営業日を返す. 長い...
code: dayafter.js
// day and holidays format yyyy-mm-dd
function isBusinessDay(day, holidays) {
if (holidays.includes(day)) return false
if (6, 7.includes(new Date(day)).getDay()) return false return true
}
// day format yyyy-mm-dd
function getTomorrow(day) {
const today = new Date(day)
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
return formatDate(tomorrow)
}
// 日付をYYYY-MM-DDの書式で返すメソッド
function formatDate(dt) {
var y = dt.getFullYear();
var m = ('00' + (dt.getMonth() + 1)).slice(-2);
var d = ('00' + dt.getDate()).slice(-2);
return (y + '-' + m + '-' + d);
}
// generator
function* gen(holidays, day, later) {
let count = 0
const exceptionalNum = 100
for (let i = 1; i < exceptionalNum; i++) {
day = getTomorrow(day)
//console.log(day)
if (holidays.includes(day) || 0, 6.includes((new Date(day)).getDay())) { //console.log('holiday')
;//do nothing
} else {
count++
}
if (count == later) break;
if( i > exceptionalNum) throw 'gen() does not work'
yield count
}
return day
}
async function main() {
const today = await fetch('now.json').then(r => r.json())
const today_str = today.today
const holidays = await fetch('holidays.json').then(r => r.json())
const g = gen(holidays, "2021-12-29", 5)
let result = g.next();
while (!result.done) {
result = g.next()
}
document.getElementById('calendar').setAttribute("min", result.value)
console.log(result)
}
document.addEventListener('DOMContentLoaded', function () {
main().then(r => console.log('done'))
})